package org.adream.account.service;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.adream.account.dao.NationDao;
import org.adream.account.entity.NationEntity;
import org.adream.account.model.ResultModel;
import org.adream.account.util.Constant;
import org.adream.account.util.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class NationService {
	
	private final static Logger logger = LoggerFactory.getLogger(NationService.class);
	
	@Autowired
	private NationDao nationDao;
	
	@Autowired
	private HttpSession session;
	
	/**
	 * 新增
	 * @param nation
	 */
	public ResultModel<String> addNation(NationEntity nation) {
		String operator = (String) session.getAttribute("uid");
		nation.setCreator(operator);
		nation.setCts(new Date());
		nation.setModifier(operator);
		nation.setMts(new Date());
		nation.setDr(Constant.ONE);
		int result = nationDao.addNation(nation);
		if(result == 0) {
			return new ResultModel<String>(ResultModel.ERR_DATABASE, "系统错误,请联系系统管理员", null);
		}
		return new ResultModel<String>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, null);
	}
	
	/**
	 * 删除 逻辑删除
	 * @param nid
	 */
	public ResultModel<String> deleteNation(Integer nid) {
		NationEntity nation = nationDao.queryNationByNidAndDr(nid, Constant.ONE);		
		if(nation == null) {
			return new ResultModel<String>(ResultModel.NOT_FOUND_OBJECT, "删除失败,数据不存在", null);
		}
		String modifier = (String)session.getAttribute("uid");
		nation.setModifier(modifier);
		nation.setMts(new Date());
		nation.setDr(Constant.ZERO);
		int result = nationDao.deleteNation(nation);
		if(result == 0) {
			return new ResultModel<String>(ResultModel.ERR_DATABASE, "系统错误,请联系系统管理员", null);
		}
		return new ResultModel<String>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, null);
	}
	
	/**
	 * 更新
	 * @param nid
	 * @param nationEntity
	 */
	public ResultModel<String> updateNation(Integer nid,NationEntity nationEntity) {
		NationEntity nation = nationDao.queryNationByNidAndDr(nid, Constant.ONE);
		if(nation == null) {
			return new ResultModel<String>(ResultModel.NOT_FOUND_OBJECT, "更新失败,数据不存在", null);
		}
		Utils.copyPropertiesIgnoreNull(nationEntity, nation);
		String modifier = (String) session.getAttribute("uid");
		nation.setModifier(modifier);
		nation.setMts(new Date());
		int result = nationDao.updateNation(nation);
		if(result == 0) {
			return new ResultModel<String>(ResultModel.ERR_DATABASE, "系统错误,请联系系统管理员", null);
		}
		return new ResultModel<String>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, null);
	}
	
	/**
	 * 根据删除标志查询
	 * @param dr 删除标志
	 * @return
	 */
	public List<NationEntity> queryNationByDr(Integer dr) {
		return nationDao.queryNationByDr(dr);
	}
	
	/**
	 * 根据nid,删除标志查询
	 * @param nid
	 * @param dr
	 * @return
	 */
	public NationEntity queryNationByNidAndDr(Integer nid,Integer dr) {
		return nationDao.queryNationByNidAndDr(nid, dr);
	}
}